package edu.nctu.csie.jichang.database.util;

import java.util.Scanner;

import edu.nctu.csie.jichang.database.model.cell.DBDatabase;
import edu.nctu.csie.jichang.database.model.cell.DBSchema;
import edu.nctu.csie.jichang.database.model.cell.DBTable;

/**
 * DebugDB
 * @author Jichang
 */
public class DebugDB {
	
	private DebugDB(){
	}
	
	public static void debug(DBDatabase ... pDatabase)throws Exception {
		int tInput =-1;
		Scanner tScanner = new Scanner(System.in);
		while(true){
			System.out.println("0 : Exit");
			for (int i = 0; i < pDatabase.length; i++) {
				System.out.println((i+1) +" : "+pDatabase[i].getDatabaseName());
			}
			System.out.print("Choose Which DataBase : ");
			try{ 
				tInput = Integer.parseInt(tScanner.next()); 
			}catch (Exception e) {
				System.out.println(e.getLocalizedMessage());
			}
			if( tInput == 0 )break;
			if( tInput > pDatabase.length ){
				System.out.println("Input Error ! ");
			}else{
				showSchema(pDatabase[tInput-1],tScanner);
			}
		}
	}
	
	/**
	 * ��� Schema
	 * @param pDataBase DataBase
	 * @param pScanner Scanner
	 * @throws Exception if Error
	 */
	private static void showSchema(DBDatabase pDataBase , Scanner pScanner)throws Exception{
		int tInput =-1;
		while(true){
			System.out.println("\n0 : Back");
			String tSchema[] = pDataBase.getSchemaNames();
			for(int i=0;i<tSchema.length;i++){
				System.out.println(i+1+" : "+tSchema[i] +" - "+pDataBase.getSchema(tSchema[i]).getCount());
			}
			System.out.print("Choose Which Schema : ");
			try{ tInput = Integer.parseInt(pScanner.next()); }catch (Exception e) {System.out.println(e.getLocalizedMessage());}
			if( tInput == 0 )return;
			else if( 0<tInput && tInput<=tSchema.length){
				showTable(pDataBase.getSchema(tSchema[tInput-1]),pScanner);
			}else{
				System.out.println("Input Error !");
			}
		}
	}

	/**
	 * ��ܪ��
	 * @param pSchema Schema
	 * @param pScanner Scanner
	 * @throws Exception if Error
	 */
	private static void showTable(DBSchema pSchema,Scanner pScanner) throws Exception{
		int tInput =-1;
		String tTableName = null;
		while(true){
			System.out.println("\n0 : Back");
			String table[] = pSchema.getTableNames();
			for(int i=0;i<table.length;i++){
				System.out.println(i+1+" : "+table[i] +" - "+pSchema.getTable(table[i]).getCount());
			}
			System.out.println("Choose Which Table : ");
			try{tInput = Integer.parseInt(pScanner.next());}catch (Exception e) {System.out.println(e.getLocalizedMessage());}
			if( tInput == 0 )return;
			else if( 0<tInput && tInput<=table.length){
				tTableName = table[tInput-1];
				System.out.println("\nTable Name = "+tTableName);
				while(true){
					System.out.println("\n0 : Back");
					System.out.println("1 : Show TableHead");
					System.out.println("2 : Show RowData");
					System.out.println("3 : Show RefConstrint(RefTable)");
					System.out.println("4 : Show NotNullColumn(RefTable)");
					System.out.println("Please Input Command : ");
					try{tInput = Integer.parseInt(pScanner.next());}catch (Exception e) {System.out.println(e.getLocalizedMessage());}
					if( tInput == 0 )break;
					switch(tInput){
						case 1:
							System.out.println(pSchema.getTable(tTableName).getTableHeader());
							break;
						case 2:
							for(String i : pSchema.getTable(tTableName).getRowNames()){
								System.out.println("IRow = "+pSchema.getTable(tTableName).getRow(i));
							}
							break;
						case 3:
						case 4:
							System.out.println("Without Implementation!");
							break;
					}
					if( tInput >4){
						System.out.println("Input Error !");
					}
				}
			}else{
				System.out.println("Input Error !");
			}
		}
	}

	public static void show(DBDatabase ... pDatabase) {
		for (DBDatabase d : pDatabase) {
			System.out.println("Database = " + d.getName());
			for (DBSchema s : d.getAllSchema()) {
				showSchema(s);
			}
		}
	}

	public static void showSchema(DBSchema ... pDBSchema) {
		for (DBSchema s : pDBSchema) {
			System.out.println("Schema = " + s.getName());
			for (DBTable t: s.getAllTables()) {
				showTable(t);
			}
		}
	}

	private static void showTable(DBTable pDBTable) {
		System.out.println("Table = " + pDBTable.getName());
		//for (ColumnInfo c : pDBTable.getTableHeader().getColumns()) {
		//	System.out.println(c.toString());
		//}
		for (String s : pDBTable.getRowNames()) {
			System.out.println(pDBTable.getRow(s).toString());
		}
	}
}
